//
//  InstallUrl.m
//  DPGJ
//
//  Created by ZhuoLaiQiang@Gmail.com on 11-11-17.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "InstallUrl.h"
#import "LFAccount.h"
#import "CoreDataManager.h"
#import "CONSTS.h"
#import "UrlParameterFactory.h"

@interface InstallUrl()

+ (void)setSystemParameter:(NSMutableDictionary **)urlParameterDic_;

@end

@implementation InstallUrl

- (void)dealloc
{
    [super dealloc];
}

+ (NSString *)getUrlWith:(NSMutableDictionary *)dicOfParameter_ addSystemParameter:(BOOL)addSystemParameterCtr api:(NSString *)api_
{
    if (addSystemParameterCtr)
    {
        [InstallUrl setSystemParameter: &dicOfParameter_];
        NSString *sign = getSign( dicOfParameter_);
        [dicOfParameter_ setObject: sign forKey: @"sign"];
    }else
    {
        NSString *sign = getLoginSign();
        [dicOfParameter_ setObject: sign forKey: @"sign"];
    }
    
    NSArray *array = nil;
	NSMutableString *requestUrl = [NSMutableString string];
	
	array = [dicOfParameter_ allKeys];
	array = [array sortedArrayUsingSelector:@selector(compare:)];
	[requestUrl setString:@""];
	for (int i = 0; i < [array count]; i++) 
    {
		if ([requestUrl length]) 
        {
			[requestUrl appendString:@"&"];
		}
		NSString *obj = [array objectAtIndex:i];
		[requestUrl appendFormat:@"%@=%@", obj, [[dicOfParameter_ objectForKey: obj] encodeURL]];
	}
    
    NSString *api_url = [NSString stringWithFormat: @"%@%@", api_, requestUrl];
    
    ITTDINFO(@"%@", api_url);
    
    return api_url;
}

+ (void)setSystemParameter:(NSMutableDictionary **)urlParameterDic_
{
    /*
     method             String	是	API接口名称
     
     session            String	否	TOP分配给用户的SessionKey，自用型应用不需要传入该参数的；非自用型的应用对于需要用户授权的API必须要传入该参数，对于不需要用户授权的API则不必传入该参数。
     
     timestamp          String	是	时间戳，格式为yyyy-MM-dd HH:mm:ss，例如：2008-01-25 20:23:30。淘宝API服务端允许客户端请求时间误差为10分钟。
     
     format             String	否	可选，指定响应格式。默认xml,目前支持格式为xml,json
     
     app_key            String	是	TOP分配给应用的AppKey
     
     v                  String	是	API协议版本，可选值:2.0。
     sign               String	是	API输入参数签名结果
     sign_method        String	是	参数的加密方法选择，可选值是：md5,hmac。这个参数只存在于2.0中。
     */
    
    CoreDataManager *coreDataManager = [CoreDataManager sharedCoreDataManager];
    LFAccount *account = [coreDataManager getCurrentLoggedInAccount];
    
    NSString *session = account.topsession;
    [*urlParameterDic_ setObject: session forKey: @"session"];
    
    NSString *timestamp = nil;
    NSDateFormatter *dataFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dataFormatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"];
    timestamp = [dataFormatter stringFromDate: [NSDate date]];
    [*urlParameterDic_ setObject: timestamp forKey: @"timestamp"];
    
    [*urlParameterDic_ setObject: @"json" forKey: @"format"];
    [*urlParameterDic_ setObject: appkey forKey: @"app_key"];
    [*urlParameterDic_ setObject: @"2.0" forKey: @"v"];
    //[*urlParameterDic_ setObject: getSign() forKey: @"sign"];
    [*urlParameterDic_ setObject: @"md5" forKey: @"sign_method"];
    
    ITTDINFO(@"%@", *urlParameterDic_);
}

@end
